package com.sea.base.fragment.opt

import android.content.Intent
import android.os.Bundle
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.viewbinding.ViewBinding
import com.sea.base.fragment.BaseKtFragment

/**
 * 实现了一次实例化的Fragment只会有一次onCreate和onDestroyView（onCreate和onCreateView、onDestroy和onDestroyView一一对应）
 * 也就是说Fragment不会因为onDestroyView然后又onCreateView导致数据可能出问题
 */
internal class FragmentItemParent internal constructor() : BaseKtFragment<ViewBinding>() {
    private var proxyFragment: FragmentItem? = null
    private var optFragmentClass: Class<FragmentItem>? = null
    private var cacheResultValue: ResultValue? = null

    internal var itemSaveData: SparseArray<Any>? = null
        private set
        get() {
            if (field == null) {
                field = SparseArray()
            }
            return field
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        @Suppress("UNCHECKED_CAST")
        optFragmentClass = Class.forName(requireArguments().getString(FragmentItem.KEY_FRAGMENT_CLASS_NAME)!!) as Class<FragmentItem>
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        proxyFragment?.platformDestroy()

        optFragmentClass!!.newInstance().let { frag ->
            frag.arguments = arguments
            proxyFragment = frag
            frag.attach(this, savedInstanceState)
            val view = frag.platformCreateView(inflater, container, savedInstanceState)
            //这里还原Result
            cacheResultValue?.let {
                frag.onActivityResult(it.requestCode, it.resultCode, it.data)
                cacheResultValue = null
            }
            return view
        }
    }

    override fun onVisible(isFirstVisible: Boolean) {
        super.onVisible(isFirstVisible)
        proxyFragment?.platformResume()
    }

    override fun onHidden() {
        super.onHidden()
        proxyFragment?.platformPause()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        proxyFragment?.platformDestroy()
        proxyFragment = null
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        //如果在界面里就直接调用，否则等待下一次create调用
        proxyFragment?.onActivityResult(requestCode, resultCode, data) ?: let { cacheResultValue = ResultValue(requestCode, resultCode, data) }
    }

    @Deprecated("请调用createFragment", ReplaceWith("createFragment(clazz: Class<T>)"))
    @RequiresApi(9999)
    override fun setArguments(args: Bundle?) {
        super.setArguments(args)
    }

    override fun init(savedInstanceState: Bundle?) {}

    override fun bindData() {}

    private class ResultValue(val requestCode: Int, val resultCode: Int, val data: Intent?)
}